package steed.ext.action.admin.wechat;

import java.util.List;
import java.util.Random;

import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.InterceptorRef;
import org.apache.struts2.convention.annotation.InterceptorRefs;
import org.apache.struts2.convention.annotation.Namespace;
import org.apache.struts2.convention.annotation.ParentPackage;

import steed.action.annotation.Power;
import steed.domain.wechat.WechatAccount;
import steed.domain.wechat.WechatUser;
import steed.exception.runtime.MessageRuntimeException;
import steed.ext.action.admin.DwzAdminAction;
import steed.hibernatemaster.util.DaoUtil;
import steed.util.base.StringUtil;
import steed.util.wechat.MutiAccountSupportUtil;
import steed.util.wechat.WechatInterfaceInvokeUtil;
import steed.util.wechat.domain.result.TemplateMessageResult;
import steed.util.wechat.domain.send.TemplateMessageSend;
@ParentPackage(value="steed")
@InterceptorRefs({@InterceptorRef(value="mydefault")})
@Namespace("/admin/wechat/sendTemplateMessage")
@Power("发送模板消息")
public class SendTemplateMessageAction extends DwzAdminAction<WechatUser>{
	private static final long serialVersionUID = 2917184309474601008L;
	private TemplateMessageSend message;
	
	public TemplateMessageSend getMessage() {
		return message;
	}
	public void setMessage(TemplateMessageSend message) {
		this.message = message;
	}
	@Action("index")
	public String index(){
		setRequestAttribute("wechatAccounts", DaoUtil.listAllCustomField(null, WechatAccount.class, "accountName","appID"));
		return steed_forward;
	}
	
	@Action("send")
	public void send(){
		final List<String> openids = DaoUtil.listAllCustomField(getModelQueryMapByRequestParam(), WechatUser.class, "openid");
		if (openids.isEmpty()) {
			throw new MessageRuntimeException("未找到符合条件的粉丝,发送失败!");
		}
		WechatAccount wechatAccount = getModel().getWechatAccount();
		MutiAccountSupportUtil.setWechatAccount(wechatAccount);
		message.setTouser(openids.get(0));
		addData();
		
		TemplateMessageResult sendTemplateMessage = WechatInterfaceInvokeUtil.sendTemplateMessage(message);
		if (!sendTemplateMessage.isSuccess()) {
			throw new MessageRuntimeException("发送失败,原因:"+sendTemplateMessage.getMessage());
		}
		
		startSendMessageTask(openids);
		
		writeClientSuccessMessage("发送成功!");
	}
	private void startSendMessageTask(final List<String> openids) {
		new Thread(new Runnable() {
			public void run() {
				WechatAccount wechatAccount = getModel().getWechatAccount();
				MutiAccountSupportUtil.setWechatAccount(wechatAccount);
				Random random = new Random();
				for (int i = 1; i < openids.size(); i++) {
					message.setTouser(openids.get(i));
					WechatInterfaceInvokeUtil.sendTemplateMessage(message);
					try {
						Thread.sleep(random.nextInt(1000)+100);
					} catch (InterruptedException e) {
						e.printStackTrace();
					}
				}
			}
		}).start();
	}
	
	private void addData() {
		String content = getRequestParameter("content2");
		if (!StringUtil.isStringEmpty(content)) {
			String[] split = content.split("\n");
			for (String temp:split) {
				if (!StringUtil.isStringEmpty(temp)) {
					int indexOf = temp.indexOf('#');
					if (indexOf == -1) {
						throw new MessageRuntimeException("您填写的模板内容有误,请检查!");
					}
					message.addData(temp.substring(0, indexOf), temp.substring(indexOf+1));
				}
			}
		}
	}
}
